<template>
  <!-- 这是新闻中心下的新闻排行榜设置页面 -->
  <div class="NewsLeader-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" icon="el-icon-edit" @click="handleAdd('News')">
          添加
        </el-button>
        <el-input v-model="search" placeholder="标题" style="width: 300px;padding-left:20px" />
      </div>
      <el-table
        :data="NewsList.filter(data => !search || data.title.toLowerCase().includes(search.toLowerCase()))"
        style="width: 100%"
        class="list-table"
      >
        <el-table-column
          prop="newsId"
          label="新闻Id"
          width="130"
          align="center"
        />
        <el-table-column
          prop="title"
          label="标题"
          width="500"
          align="center"
        />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleClick(scope.row.newsId)"
            >查看</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.newsId)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogForm">
      <el-form :model="form">
        <el-form-item label="新闻Id" :label-width="formLabelWidth">
          <el-input v-model="News.newsId" autocomplete="off" />
        </el-form-item>
        <!-- <el-form-item label="新闻类型" :label-width="formLabelWidth">
          <el-input v-model="News.newsType" autocomplete="off" />
        </el-form-item>
        <el-form-item label="新标题" :label-width="formLabelWidth">
          <el-input v-model="News.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="来源" :label-width="formLabelWidth">
          <el-input v-model="News.newsSource" autocomplete="off" />
        </el-form-item>
        <el-form-item label="内容" :label-width="formLabelWidth">
          <el-input v-model="News.content" autocomplete="off" type="textarea" :rows="10" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="handleCancel('News')">取 消</el-button>
        <el-button type="primary" @click.native="handleUpdate('News')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getNewsLeader,
  addNewsLeader,
  deleteNewsLeader
} from '@/api/News'

export default {
  data() {
    return {
      NewsList: [{
        newsId: '',
        title: ''
      }],
      News: {
        newsId: '',
        newsSource: '',
        title: '',
        content: '',
        newsType: ''
      },
      search: '',
      dialogForm: false,
      titleMap: {
        add: '新增新闻'
      },
      dialogStatus: ''
    }
  },

  created() {
    this.loadNewsLeader()
  },

  methods: {
    // 获取新闻排行榜列表
    loadNewsLeader() {
      getNewsLeader().then(res => {
        console.log(res)
        this.NewsList = res.data
        // this.totalpages = res.data[0].pages * 10
      })
    },
    // onCurrentChange(page) {
    //   this.loadNewsLeader(page)
    // },
    // 通过新闻ID添加新闻到排行榜
    handleAdd: function(News) {
      this.dialogForm = true
      this.dialogStatus = 'add'
      this.News.newsId = News.newsId
      this.News.newsSource = News.newsSource
      this.News.title = News.title
      this.News.content = News.content
      this.News.newsType = News.newsType
    },

    handleDelete: function(newsId) {
      // 将新闻移除排行榜
      this.$confirm('确认删除?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认执行
        console.log(newsId)
        new Promise((resolve, reject) => {
          deleteNewsLeader({ newsId: newsId }).then(res => {
          // 删除成功
            this.loadNewsLeader()
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 更新信息
    handleUpdate(forName) {
      addNewsLeader(this.News).then(res => {
        console.log(this.News)
        console.log(res)
        this.loadNewsLeader()
      })

      this.$message({
        type: 'success',
        message: '创建成功'
      })

      this.dialogForm = false
    },
    // 取消修改
    handleCancel(formName) {
      this.$message({
        type: 'info',
        message: '已取消'
      })
      this.dialogForm = false
    },
    handleClick(newsId) {
      // windows.open('https://www.baidu.com/')
    }

  }
}
</script>

<style>

</style>
